package com.shipwe.loans.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.shipwe.loans.MyApps
import com.shipwe.loans.R
import com.shipwe.loans.databinding.DialogUserDataClearBinding
import com.shipwe.loans.pro.ShipweDialog
import com.shipwe.loans.utils.event.AppEventPointHelper
import com.shipwe.loans.utils.event.AppEventPointType
import com.shipwe.loans.utils.thread.TimingThread

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/27 15:50
 * @Annotation：清空用户数据dialog
 */
class UserDataClearDialog: ShipweDialog, TimingThread.ITimingThreadListener {

    private lateinit var binding: DialogUserDataClearBinding
    private var mCallback: IDialogCallback? = null

    //倒计时
    private var countdown = 10
    private var timeThread: TimingThread? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogUserDataClearBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {

    }

    override fun initListener() {
        setOnClickListener(binding.clearDataReturn)
        setOnClickListener(binding.clearDataConfirm)
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        dismiss()
        when(v!!.id){
            R.id.clearDataConfirm -> {
                if(mCallback != null){
                    mCallback!!.onUserDataClearConfirmClick()
                }
            }
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onUserDataClearConfirmClick()
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        countdown = 10
        checkClearUserDataConfirmStatus()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 0) {
            checkClearUserDataConfirmStatus()
            countdown = 10
            timeThread!!.stopThread()
            return
        }
        countdown--
        checkClearUserDataConfirmStatus()
    }

    @SuppressLint("SetTextI18n")
    private fun checkClearUserDataConfirmStatus(){
        if (countdown <= 0){
            binding.clearDataConfirm.isEnabled = true
            binding.clearDataConfirm.text = MyApps.app.getString(R.string.click_confirm)
        } else {
            binding.clearDataConfirm.isEnabled = false
            binding.clearDataConfirm.text = "${MyApps.app.getString(R.string.click_confirm)}(${countdown}s)"
        }
    }

    override fun onStart() {
        super.onStart()
        startTimeThread()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_USER_DATA_CLEAR_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_USER_DATA_CLEAR_DISMISS)
    }
}